package com.example.carrierapp.ui.adapter;

import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.carrierapp.bean.find.FindGoodsListBean;
import com.example.carrierapp.ui.view.recycleview.XRecyclerView;
import com.example.carrierapp.util.DateUtils;
import com.example.carrierapp.util.LogUtil;
import com.example.lb.fingerdemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FindGoodsAdatpter extends XRecyclerView.Adapter<FindGoodsAdatpter.ViewHolder> {
    private List<FindGoodsListBean.DispatchInfo> mList = new ArrayList();

    public FindGoodsAdatpter() {
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_find_goods, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        FindGoodsListBean.DispatchInfo info = mList.get(position);
        holder.bindData(info);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addDataNotClear(List<FindGoodsListBean.DispatchInfo> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addData(List<FindGoodsListBean.DispatchInfo> list) {
        if (list != null && list.size() > 0) {
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }


    public interface OnBtnClickListener {
        void onDetailClick(FindGoodsListBean.DispatchInfo info);
    }

    private OnBtnClickListener mOnItemClickListener;

    public void setOnBtnClickListener(OnBtnClickListener listener) {
        mOnItemClickListener = listener;
    }


    public class ViewHolder extends XRecyclerView.ViewHolder {
        @BindView(R.id.iv_find_short)
        ImageView mIvShort;
        @BindView(R.id.tv_find_address_start)
        TextView mTvStart;
        @BindView(R.id.tv_find_address_end)
        TextView mTvEnd;
        @BindView(R.id.tv_find_goods)
        TextView mTvGoods;
        @BindView(R.id.tv_find_volume)
        TextView mTvVolume;
        @BindView(R.id.tv_find_time)
        TextView mTvTime;
        @BindView(R.id.tv_find_btn)
        TextView mTvBtn;
        @BindView(R.id.tv_find_price)
        TextView mTvPrice;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(FindGoodsListBean.DispatchInfo info) {
            if (info == null) {
                LogUtil.d("info is null");
                return;
            }
            mTvStart.setText(info.startCity);
            mTvEnd.setText(info.endCity);
            mTvGoods.setText(info.proName);
            mTvVolume.setText("总运量" + info.proNum + info.unitName);
            mTvTime.setText(DateUtils.creatMonthDay(info.createDate));
            if(info.offer!=0)
            {
                mTvPrice.setText(info.offer+"元/"+info.unitName);
            }
            //抢竞模式 1竞价2抢单 = ['1', '2'], (integer, optional):
            mIvShort.setVisibility(info.shortDistance == 1 ? View.VISIBLE : View.GONE);
            mTvBtn.setText(info.operationTip);
            mTvBtn.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onDetailClick(info);
                }
            });
        }
    }
}
